package com.qjdchina.sku.biz.impl;

import com.qjdchina.commons.enums.IsDeleted;
import com.qjdchina.commons.result.Result;
import com.qjdchina.sku.biz.UserManager;
import com.qjdchina.sku.consts.WebConsts;
import com.qjdchina.sku.dao.mapper.UserMapper;
import com.qjdchina.sku.model.User;
import com.qjdchina.sku.service.enums.SkuResultCode;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;

import javax.servlet.http.HttpSession;


/**
 * Created by fankiwu on 2016/12/1.
 */
@Service
public class UserManagerImpl implements UserManager {

    public static final Logger LOGGER = LoggerFactory.getLogger(UserManagerImpl.class);

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result<User> login(@RequestParam(value="username",required = true)String userName,
                              @RequestParam(value="password",required = true)String password, HttpSession httpSession) {

        // 参数判断
        if(StringUtils.isBlank(userName) || StringUtils.isBlank(password)){
            LOGGER.warn("param is blank");
            return new Result<User>(SkuResultCode.PARAMETER_NOT_EMPTY);
        }

        // 用户不存在
        User user = userMapper.selectByNameAndPass(userName, password);
        if(null == user){
            LOGGER.warn("user cant find !");
            return new Result<User>(SkuResultCode.USER_CANT_FIND);
        }

        // 设置相关session属性
        setSessionAtribute(httpSession, user);

        return new Result<User>(user);
    }

    /**
     * 设置session属性
     * @param httpSession
     * @param user
     */
    private void setSessionAtribute(HttpSession httpSession, User user) {

        httpSession.setAttribute(WebConsts.USER_NAME, user.getUserName());
        httpSession.setAttribute(WebConsts.USER_ID, user.getUserId());
        httpSession.setAttribute(WebConsts.COMPANY_ID, user.getCompanyId());
        httpSession.setAttribute(WebConsts.USER_CODE, user.getUserCode());
        httpSession.setAttribute(WebConsts.USER_TYPE,user.getUserType());
    }
    
    /**
     * 根据用户名密码查询
     * shenpingfeng
     * 2016年12月16日
     * @param userName
     * @param password
     * @return
     */
    @Override
    public Result<User> getUserByUserName(String userName,String password) {

        // 参数判断
        if(StringUtils.isBlank(userName) || StringUtils.isBlank(password)){
            LOGGER.warn("param is blank");
            return new Result<User>(SkuResultCode.PARAMETER_NOT_EMPTY);
        }

        // 用户不存在
        User user = userMapper.selectByNameAndPass(userName, password);
        if(null == user){
            LOGGER.warn("user cant find !");
            return new Result<User>(SkuResultCode.USER_CANT_FIND);
        }

        return new Result<User>(user);
    }
    
    /**
	 * 更新
	 * shenpingfeng
	 * 2017年1月10日
	 * @param user
	 * @return
	 */
    @Override
    public Result<String> update(User user) {
    	if(user == null){
    		return new Result<String>(SkuResultCode.PARAMETER_NOT_EMPTY);
    	}
    	user.setGmtModified(new Date());
    	int row = userMapper.updateByPrimaryKeySelective(user);
    	if(row == 0){
    		return new Result<String>(SkuResultCode.UPDATE_FAIL);
    	}
        return new Result<String>(user.getPassword());
    }
    /**
	 * 创建
	 * shenpingfeng
	 * 2017年1月12日
	 * @param user
	 * @return
	 */
    @Override
    public Result<String> create(User user) {
    	if(user == null){
    		return new Result<String>(SkuResultCode.PARAMETER_NOT_EMPTY);
    	}
    	user.setGmtCreated(new Date());
    	user.setGmtModified(new Date());
    	user.setIsDeleted(IsDeleted.NO.getValue());
    	int row = userMapper.insertSelective(user);
    	if(row == 0){
    		return new Result<String>(SkuResultCode.ADD_FAIL);
    	}
        return new Result<String>(user.getPassword());
    }
}
